package com.web.manager.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.pojo.Court;
import com.web.manager.pojo.Result;
import com.web.manager.service.CourtService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/court")
public class CourtController {
    @Resource
    private CourtService courtService;




    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody Court court){
            courtService.save(court);  //insertUser//save==>插入一个信息新增
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Court court){
        courtService.updateById(court);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){


        courtService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        courtService.removeBatchByIds(ids);   //batchDeleteUser
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Court> userList = courtService.list();  //selectAll
        return Result.success(userList);
    }



    //根据id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Court court= courtService.getById(id);   //selectById
        return Result.success(court);
    }



    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String username,
                               @RequestParam String name) {

        QueryWrapper<Court> queryWrapper = new QueryWrapper<Court>();  //条件构造器
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        //select * from manager_user where username like '%#{username}% and name like '%#{name}%'

        Page<Court> page = courtService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }
}
